<?php
declare(strict_types=1);

namespace App\ZxAdmin\Sys\Stat;

use App\Controller;

// hook controller_zx_admin__sys_stat_table_use.php

/**
 * @title   SwooleTable
 * @auth    true
 * @login   true
 * @menu    true
 * @component vlist/index.vue
 * @icon    el-icon-menu
 * @active  true
 * @affix   false
 * @rank    3
 */
class Table extends Controller
{
    // hook controller_zx_admin__sys_stat_table_public_start.php


    /**
     * @title   列表配置
     */
    public const Conf = [
        'pageSize' => 25,
        'remoteSort'=>0,
        'remoteFilter'=>0,
        'remoteSummary'=>0,
        'hidePagination'=>1,
        'hideRefresh'=>0,
        'hideSetting'=>0,
        'hideDo'=>0,
        'add'=>0,
        'start'=>0,
        'stop'=>0,
        'delete'=>1,
        'import'=>0,
        'export'=>0
    ];

    // hook controller_zx_admin__sys_stat_table_public_end.php

    /**
     * @title  下发配置
     * @rank    0
     */
    public function action_Conf_PUT()
    {
        // hook controller_zx_admin__sys_stat_table_conf_start.php
        $Columns = [
            [
                'label' => "键",
                'prop' => "k",
                'width' => "300px",
            ],
            [
                'label' => "值",
                'prop' => "v",
                'flex' => 1,
                'width' => '600px',
                'type' =>'kv',
                'autoHeight' =>true,
                'readonly'=>true,
            ]
        ];

        $tabs = [];
        foreach ($_ENV['Table'] as $k => $v) {
            $tabs[] = [
                'Name' => $k,
                'Index' => $k
            ];
        }
        // make conf SysDict ZxAdmin\Sys
        // hook controller_zx_admin__sys_stat_table_conf_end.php
        $this->response('0000', ['Tabs' => $tabs,'Conf' => self::Conf, 'Filter' => [], 'Tools' => [], 'Columns' => $Columns, 'data' => []]);
    }

    /**
     * @title   下发列表数据
     * @rank    73
     */
    public function action_Index_GET()
    {
        // hook controller_zx_admin__sys_stat_table_index_start.php
        $tab = $this->request->param('tab','WorkersStat');
        $table = [];
        $t = $this->_T($tab);
        foreach ($t as $k => $row){
            $table[]=['k'=>$k,'v'=>  $t->get($k)];
        }

        // hook controller_zx_admin__sys_stat_table_index_end.php
        $this->response('0000', ['data'=>$table,'bottom'=>[['k'=>'表统计','v'=>xn_json_encode($t->stats())]]]);
    }

    /**
     * @title   编辑字段
     * @rank    60
     */
    public function action_Field_POST()
    {
        // hook controller_zxadmin_content_cate_field_post_start.php

        $tab = $this->request->param('tab', '');
        $k = $this->request->param('k', '');
        $value = $this->request->param('value', '');
        $this->CheckEmpty([$tab,$k,$value], ['待修改字段','待修改字段','待修改字段']);
        $valueUpdate  = xn_json_decode($value);
        $this->_T($tab)->set($k.'',$valueUpdate);

        // hook controller_zxadmin_content_cate_field_post_end.php
        $this->response( '0000');
    }


    /**
     * @title   删除数据
     * @rank    50
     */
    public function action_Index_Delete()
    {
        // hook controller_zx_admin_sys_sys_dict_delete_start.php
        $tab = $this->request->param('tab', '');
        $data = $this->request->param('data', []);
        $this->CheckEmpty([$tab,$data], ['操作表','操作数据为空']);
        foreach ($data as $row){
            $this->_T($tab)->delete($row['k']);
        }
        // hook controller_zx_admin_sys_sys_dict_delete_end.php
        $this->response( '0000');
    }

    // hook controller_zx_admin__sys_stat_table_end.php
}

?>